package com.edu.controller;


import com.edu.dao.ProductCategoryDao;
import com.edu.dao.ProductDao;
import com.edu.data.ProductCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/Category")
public class CategoryController {
    @Autowired
    ProductCategoryDao category;

    @Autowired
    ProductDao productDao;

    @RequestMapping("/list")
    public String list(Model model){
        List<ProductCategory> productCategoryList = category.getList();
        model.addAttribute("categories",productCategoryList);
        return "../../manage/productClass";
    }
    @RequestMapping("/add")
    public String add(Model model){
        List<ProductCategory> productCategoryList = category.getList();
        model.addAttribute("categories",productCategoryList);
        return "../../manage/productClass-modify";
    }
    @RequestMapping("/save")
    public String save(String parentId,String Text){
        int max=category.selectnum();
        int Id=Integer.parseInt(parentId);
        max++;
        System.out.println(""+Text);
        ProductCategory productCategory=null;

        if(parentId.equals("0")){
            productCategory=new ProductCategory();
            productCategory.setEpcId(max);
            productCategory.setEpcName(Text);
            productCategory.setEpcParentId(max);
            category.parentAdd(productCategory);//添加父类别

        }else {
            productCategory=new ProductCategory();
            productCategory.setEpcId(Id);
            productCategory.setEpcName(Text);
            productCategory.setEpcParentId(Id);
            category.childAdd(productCategory);//添加子类别
        }
        return "redirect:/Category/list";
    }
    @RequestMapping("/delect")
    public String delect(Model model,String id,String ids){
        int epid=Integer.parseInt(id);
        int epcid=Integer.parseInt(ids);
        if(epid==epcid){
            productDao.delectproduct(epid);
            category.delectsproductcategory(epcid);
        }else{
            productDao.delectproduct(epid);
            category.delectsproductcategory(epid);
        }
        List<ProductCategory> productCategoryList = category.getList();
        model.addAttribute("categories",productCategoryList);
        return "redirect:/Category/list";
    }
    @RequestMapping("/update")
    public String update(Model model,int id) throws SQLException {
        ProductCategory productCategory= category.findById(id);
        List<ProductCategory> productCategoryList = category.getList();
        model.addAttribute("productCategory",productCategory);
        model.addAttribute("categories",productCategoryList);
        return "../../manage/productClass-update";
    }
    @RequestMapping("/updates")
    public String updates(HttpServletRequest req, Model model, String epcid, String parentId,String name) throws SQLException {
        int id=Integer.parseInt(epcid);
        int ids=Integer.parseInt(parentId);
        System.out.println(""+id);
        System.out.println(""+ids);
        Map<String,String> errors=new HashMap<String,String>();
        List<ProductCategory> productCategoryList = category.getList();
        int x=0;
        for(int i = 0; i<productCategoryList.size();i++){
            if(productCategoryList.get(i).getEpcName().equals(name)){
                x=1;
            }
        }
        if(x==1){
            errors.put("error_name","分类名已存在");
        }

        if(errors.size()>0){
            req.setAttribute("errors",errors);
            return "../../manage/productClass-update";
        }else{
            ProductCategory productCategory=null;
            productCategory=new ProductCategory();
            productCategory.setEpcId(id);
            productCategory.setEpcName(name);
//            productCategory.setEpcParentId(max);
            category.update(productCategory);

            return "redirect:/Category/list";
        }
    }

}
